package edu.jhu.cs.oose.scheduleit.dao.impl;

import java.util.LinkedList;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import edu.jhu.cs.oose.scheduleit.dao.EventDao;
import edu.jhu.cs.oose.scheduleit.util.DateWrapper;
import edu.jhu.cs.oose.scheduleit.vo.Event;
import edu.jhu.cs.oose.scheduleit.vo.Schedule;
import edu.jhu.cs.oose.scheduleit.vo.UserInfo;
/**
 * The implementation class of the EventDao interface
 * @author oosegroup2
 *
 */
public class EventDaoImpl implements EventDao{
	
	private HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	@Override
	public void save(Event event){
		this.hibernateTemplate.saveOrUpdate(event);
	}
	
	@Override
	public void addJoinedUser(Event event, UserInfo userInfo){
		event.addUser(userInfo);
		this.hibernateTemplate.saveOrUpdate(event);
	}
	
	public void removeJoinedUser(Event event, UserInfo userInfo){
		event.removeUser(userInfo);
		this.hibernateTemplate.saveOrUpdate(event);
	}
	@SuppressWarnings("unchecked")
	@Override
	public Event getEvent(int eventID){
		List<Event> result = this.hibernateTemplate.find(" from Event Where id ="+eventID);
		return result.size() == 0? null:result.get(0);		
	}
	
	@Override
	public void deleteEvent(Event event){
		this.hibernateTemplate.delete(event);
	}

	@Override
	public int count(Schedule schedule, DateWrapper startTime, DateWrapper endTime) {
		int totalCount = 0;
		String queryString ="Select count(e) from Schedule as s inner join s.events as e where s.id = ? and e.startTime >=? and e.endTime <=?";
		totalCount = Integer.parseInt(this.hibernateTemplate.find(queryString,new Object[]{schedule.getId(),startTime.getTime(),endTime.getTime()}).get(0).toString());
		for (Schedule s: schedule.getMergedSchedules()){
			totalCount += Integer.parseInt(this.hibernateTemplate.find(queryString,new Object[]{s.getId(),startTime.getTime(),endTime.getTime()}).get(0).toString());
		}
		return totalCount;
	}

	@Override
	public int count(Schedule schedule){
		int totalCount = 0;
		String queryString ="Select count(e) from Schedule as s inner join s.events as e where s.id = ?";
		totalCount += Integer.parseInt(this.hibernateTemplate.find(queryString,new Object[]{schedule.getId()}).get(0).toString());
		for (Schedule s: schedule.getMergedSchedules()){
			totalCount += Integer.parseInt(this.hibernateTemplate.find(queryString,new Object[]{s.getId()}).get(0).toString());			
		}
		return totalCount;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Event> getEvents(Schedule schedule, DateWrapper startTime, DateWrapper endTime) {		
		List<Event> totalResult = new LinkedList<Event>();
		String queryString ="Select e from Schedule as s inner join s.events as e where s.id = ? and e.startTime >= ? and e.endTime <=? order by e.id";
		
		totalResult.addAll(this.hibernateTemplate.find(queryString,new Object[]{schedule.getId(),startTime.getTime(),endTime.getTime()}));
		for (Schedule s: schedule.getMergedSchedules()){
			totalResult.addAll(this.hibernateTemplate.find(queryString,new Object[]{s.getId(),startTime.getTime(),endTime.getTime()}));			
		} 
		return totalResult;
		
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Event> getEvents(Schedule schedule){
		List<Event> totalResult = new LinkedList<Event>();
		String queryString ="Select e from Schedule as s inner join s.events as e where s.id = ? order by e.id";
		totalResult.addAll(this.hibernateTemplate.find(queryString,new Object[]{schedule.getId()}));
		for (Schedule s: schedule.getMergedSchedules()){
			totalResult.addAll(this.hibernateTemplate.find(queryString,new Object[]{s.getId()}));			
		}
		return totalResult;
	}
}
